﻿//----------------------------------------------------------------
// Desc: 实体类对象包装
// Edit: ShowLong, http://blog.csdn.net/showlong
//
// Date Created: 2010-06-11
//
// Update Date : 
//
// Copyright (c) Fuel4D (http://www.fuel4d.com), All rights reserved.
//
// 实用性实例类，可以明确知道该类的创建和析构时间，便于内存检查
// 使用方法： class MyManagerClass : public IFInstance<MyManagerClass>
// 
//----------------------------------------------------------------
#pragma once


template < typename _Ty >
class IFInstance : public IFMemObject
{
    static _Ty* fclass_instance;

protected:
    IFInstance()
    {
        FASSERTA(!fclass_instance, FAssertFmtA(_FTA("重复初始化或者构造(%s)实体类"), _Ty::_StaticClassName()));
        if (!fclass_instance)
            fclass_instance = (_Ty*)this;
    }
    IFInstance(const IFInstance&){}
    IFInstance& operator=(const IFInstance&){ return *this; }

public:
    virtual~IFInstance()
    {
        FASSERTA(fclass_instance, FAssertFmtA(_FTA("重复释放(%s)实体类"), _Ty::_StaticClassName()));
        fclass_instance = 0;
    }
    // 测试是否已经创建了实例了
    static bool IsInitialize()
    {
        return fclass_instance != 0;
    }
    // 获取实例地址
    static _Ty* Instance()
    {
        FASSERT(fclass_instance, _FT("没有找到实体内存地址"));
        return fclass_instance;
    }
    // 获取实例对象
    static _Ty& GetInstance()
    {
        FASSERT(fclass_instance, _T("没有找到实体内存地址"));
        return *fclass_instance;
    }
};

template <typename _Ty>
_Ty* IFInstance<_Ty>::fclass_instance = 0;